package com.whyx.project.openapi.map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whyx.project.openapi.business.DistributeTotality;
import com.whyx.project.openapi.config.RedisConfig;
import com.whyx.project.openapi.controller.KafkaApiController;
import exsun.jt808.data.base.StatusResult;
import exsun.jt808.data.config.jt808.Jt808MsgIdTopicDic;
import exsun.jt808.data.distribute_data.DistributeEntity;
import exsun.jt808.data.enume.OrderHttpStatus;
import exsun.jt808.data.enume.OrderSendStatus;
import exsun.jt808.data.util.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class MapUtil extends OrderSendStatus2HttpStatus {
    private final static Logger logger = LoggerFactory.getLogger(MapUtil.class);

    private static RedisConfig redisConfig;

    @Autowired
    public void setRedisConfig(RedisConfig redisConfig) {
        MapUtil.redisConfig = redisConfig;
    }


//    public Map InsertDatabases(DistributeTotality distributeTotality, DistributeEntity distributeEntity) {
//        Map<String, Object> map1 = new HashMap<String, Object>();
//        map1.put("orderId", distributeTotality.getOrderId());
//        map1.put("vehicleId", distributeTotality.getVehicleId());
//        map1.put("vehicleNum", distributeTotality.getVehicleNum());
//        map1.put("deviceNo", distributeTotality.getDeviceNo());
//        map1.put("content", JSONObject.parseObject(distributeTotality.getContent()));
//        map1.put("confValue", JSONObject.parseArray(distributeTotality.getConfValue()));
//        map1.put("msgId", Jt808MsgIdTopicDic.getMsgId(distributeEntity.distributeMsgTopic));
//        map1.put("status", new StatusResult(OrderHttpStatus.待下发, new Object()).getCode());
//        map1.put("failReason", null);
//        map1.put("responseResult", null);
//        map1.put("createUserId", distributeTotality.getUserId());
//        return map1;
//    }

    public Map UpdateDatabases(StatusResult result) {
        Map<String, Object> map2 = new HashMap<String, Object>();
//        if (result == null) {
//            map2.put("status", 5);
//            map2.put("failReason", "设备应答超时");
//            map2.put("responseResult", null);
//        } else {
            String msg = getOrderHttpStatusMsg(result.getCode());
            int code = getOrderSendStatusCode(result.getCode());
            JSONObject obj = JSONObject.parseObject(JSON.toJSONString(result.getResult()));
            map2.put("status", code);
            map2.put("failReason", (code == 4 || code == 1) ? "" : msg);
            map2.put("responseResult", obj);
//        }
        return map2;
    }

    public Map UpdateCommonDatabases(StatusResult result) {
        Map<String, Object> map2 = new HashMap<String, Object>();
        String msg = getOrderHttpStatusMsg(result.getCode());
        int code = getOrderSendStatusCode(result.getCode());
        JSONObject obj = JSONObject.parseObject(JSON.toJSONString(result.getResult()));
        map2.put("status", code);
        map2.put("failReason", (code == 3 || code == 1) ? "" : msg);
        map2.put("responseResult", obj);
//        map2.put("msgId",obj.get("msgId"));
        return map2;
    }

    public Map UpdateUpgradeDatabases(StatusResult result, String vNumber, String devNo) {
        Map<String, Object> map2 = new HashMap<String, Object>();
        String msg = getOrderHttpStatusMsg(result.getCode());
        int code = getOrderSendStatusCode(result.getCode());
        JSONObject obj = JSONObject.parseObject(JSON.toJSONString(result.getResult()));
        map2.put("status", code);
        map2.put("failReason", (code == 4 || code == 1) ? "" : msg);
        map2.put("responseResult", obj);
        map2.put("vNumber", vNumber);
        map2.put("deviceNo", devNo);
        return map2;
    }
}
